import unittest
from time import sleep

from selenium.webdriver.common.by import By

from page import l_config_front_end as l_conf
import parameterized
from page.l_page_front_end import PageFrontEnd
from tools.l_read_data import read_data


def get_data(value):
    datas = read_data("../data/l_search_data.json")
    # 新建空列表
    arrs = []
    for data in datas.values():
        arrs.append((data['search_value'], data[value]))
    return arrs


class TestSearch(unittest.TestCase):
    front = None

    @classmethod
    def setUpClass(cls) -> None:
        cls.front = PageFrontEnd()

    # 点击搜索测试
    @parameterized.parameterized.expand(get_data("expect_search_value"))
    def test_search01(self, search_value, expect_search_value):
        self.front.page_search_text(search_value)
        msg = self.front.page_if_exist()
        try:
            self.assertEqual(expect_search_value, msg)
        except AssertionError:
            self.front.get_image('l-s1-')
            self.front.page_back_home()
            raise


    # 购买商品测试
    @parameterized.parameterized.expand(get_data('expect_submit_value'))
    def test_search02(self, search_value,submit_value):
        self.front.driver.implicitly_wait(1)
        self.front.page_search_text(search_value)
        goods = self.front.driver.find_elements(By.XPATH,'/html/body/div[4]/div/ul/li')
        if len(goods) != 0:
            try:
                self.front.page_click_goods()
                self.front.page_go_new()
                self.front.driver.find_element(*l_conf.good_info)
            except:
                self.front.page_click_select()
                try:
                    self.front.page_buy_num()
                    self.front.page_buy_good()
                    self.front.page_good_payment()
                    self.front.page_submit_order_for_good()
                    msg4 = self.front.page_get_error_info()
                    self.assertEqual(submit_value, msg4)
                except:
                    self.front.get_image('l-s2-')
                    self.front.page_back_home()
                    self.front.page_go_new()
                    raise


    # 加入购物车测试
    @parameterized.parameterized.expand(get_data('expect_add_shopping_cart_value'))
    def test_search03(self,search_value,expect_add_shopping_cart_value):
        self.front.driver.implicitly_wait(1)
        self.front.page_search_text(search_value)
        goods = self.front.driver.find_elements(By.XPATH,'/html/body/div[4]/div/ul/li')
        if len(goods) != 0:
            try:
                self.front.page_click_goods()
                self.front.page_go_new()
                self.front.driver.find_element(*l_conf.good_info)
            except:
                # 测试加入购物车
                try:
                    self.front.page_click_select()
                    self.front.page_add_shopping_cart()
                    msg1 = self.front.page_get_error_info()
                    self.assertEqual(expect_add_shopping_cart_value, msg1)
                except:
                    self.front.get_image("l-s3-")
                    raise
    # 点击收藏测试
    @parameterized.parameterized.expand(get_data('expect_good_collect_value'))
    def test_search04(self,search_value,expect_good_collect_value):
        self.front.driver.implicitly_wait(1)
        self.front.page_search_text(search_value)
        goods = self.front.driver.find_elements(By.XPATH, '/html/body/div[4]/div/ul/li')
        if len(goods) != 0:
            try:
                self.front.page_click_goods()
                self.front.page_go_new()
                self.front.driver.find_element(*l_conf.good_info)
            except:
                # 测试加入收藏
                try:
                    self.front.page_good_collect()
                    msg2 = self.front.page_get_error_info()
                    self.assertEqual(expect_good_collect_value, msg2)
                except:
                    self.front.get_image("l-s4-")
                    raise


    @classmethod
    def tearDownClass(cls) -> None:
        sleep(3)
        cls.front.driver.quit()
